#pragma once
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <memory>
#include <pthread.h>
#include <unistd.h>
#include "MyThread.hpp"
#include "Log.hpp"

const static int gdefaultthreadnum = 3;

template <class T>
class ThreadPool
{
private:
    ThreadPool(int threadnum = gdefaultthreadnum)
        : _threadnum(threadnum), _waitnum(0), _isrunning(false)
    {
        pthread_mutex_init(&_mutex, nullptr);
        pthread_cond_init(&_cond, nullptr);
        LOG(INFO, "ThreadPool Construct()");
    }

    ThreadPool<T> &operator=(const ThreadPool<T> &) = delete;
    ThreadPool(const ThreadPool<T> &) = delete;

    void HandlerTask(std::string &name)
    {
        LOG(INFO, "%s is running...", name.c_str());
        while (true)
        {
            pthread_mutex_lock(&_mutex);
            while (_task_queue.empty() && _isrunning)
            {
                _waitnum++;
                pthread_cond_wait(&_cond, &_mutex);
                _waitnum--;
            }
            if (_task_queue.empty() && !_isrunning)
            {
                pthread_mutex_unlock(&_mutex);
                break;
            }
            T t = _task_queue.front();
            _task_queue.pop();
            pthread_mutex_unlock(&_mutex);
            LOG(DEBUG, "%s get a task", name.c_str());
            t();
            LOG(DEBUG, "%s has made a task", name.c_str());
        }
    }

    void InitThreadPool()
    {
        for (int i = 0; i < _threadnum; i++)
        {
            std::string name = "thread-" + std::to_string(i + 1);
            _threads.emplace_back(std::bind(&ThreadPool::HandlerTask, this,name), name);
            LOG(INFO, "init thread %s done", name.c_str());
        }
        _isrunning = true;
    }
    void Start()
    {
        for (auto &thread : _threads)
        {
            thread.start();
        }
    }

public:
    static ThreadPool<T> *Getinstance(int num = 3)
    {
        if (_instance == nullptr)
        {
            pthread_mutex_lock(&_lock);
            if (_instance == nullptr)
            {
                _instance = new ThreadPool<T>(num);
                _instance->InitThreadPool();
                _instance->Start();
                LOG(DEBUG, "创建单例成功");
                return _instance;
            }
            pthread_mutex_unlock(&_lock);
        }
        LOG(DEBUG, "获取单例成功");
        return _instance;
    }

    void Stop()
    {
        pthread_mutex_lock(&_mutex);
        _isrunning = false;
        pthread_cond_broadcast(&_cond);
        pthread_mutex_unlock(&_mutex);
    }
    bool Enqueue(const T t)
    {
        bool ret = false;
        pthread_mutex_lock(&_mutex);
        if (_isrunning)
        {
            _task_queue.push(t);
            if (_waitnum > 0)
            {
                pthread_cond_signal(&_cond);
            }
            LOG(DEBUG, "enqueue task success");
            ret = true;
        }
        pthread_mutex_unlock(&_mutex);
        return ret;
    }
    void Wait()
    {
        for (auto &thread : _threads)
        {
            thread.join();
            LOG(INFO, "%s is quit...", thread.name().c_str());
        }
    }
    ~ThreadPool()
    {
        pthread_mutex_destroy(&_mutex);
        pthread_cond_destroy(&_cond);
    }

private:
    int _threadnum;
    std::vector<MyThread> _threads;
    std::queue<T> _task_queue;
    pthread_mutex_t _mutex;
    pthread_cond_t _cond;
    int _waitnum;
    bool _isrunning;

    static ThreadPool<T> *_instance; // 懒汉就是要创建指针
    static pthread_mutex_t _lock;
};
template <class T>
ThreadPool<T> *ThreadPool<T>::_instance = nullptr;
template <class T>
pthread_mutex_t ThreadPool<T>::_lock = PTHREAD_MUTEX_INITIALIZER;